<?php

namespace app\api\controller\haibao;

use app\admin\model\HaibaoAcc;
use app\admin\model\HaibaoFavor;
use app\admin\model\HaibaoVideo;
use app\admin\model\Xcx;
use app\common\controller\Api;

/**
 * 嗨爆小片用户相关接口
 */
class Accs extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        $xcx = Xcx::get(['ghid' => $gh_id]);

        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $params['appid'] = $xcx['appid'];
        $params['secret'] = $xcx['secret'];
        $params['js_code'] = $code;
        $params['grant_type'] = 'authorization_code';

        $res = $this->http($url, $params);
        $json = json_decode($res, true);
        $msg = $json['openid'];

        if ($acc = HaibaoAcc::get(['openid' => $json['openid']])) {
            $begin_today=mktime(0,0,0,date('m'),date('d'),date('Y'));
            if ($acc->prev_formid < $begin_today) {
                $acc->is_formid = 1;
            }
            $acc->save();
            $data['userid'] = $acc->id;
            $data['lock'] = $acc->lock;
            $data['is_formid'] = $acc->is_formid ? true : false;
        } else {
            $a = new HaibaoAcc;
            $a->openid = $json['openid'];
            $a->lock = 0;
            $a->save();
            $data['userid'] = $a->id;
            $data['lock'] = $a->lock;
            $data['is_formid'] = true;
        }

        $this->result($msg, $data);
    }

    // 投诉接口
    public function complaint()
    {
        return 'true';
    }

    // 配置
    public function config()
    {
        $flags = array(
            'videoflag' => true,
            'adflag' => false,
            'txflag' => true,
        );
        $this->result('flag', $flags);
    }

    public function fetch_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        if ($f = HaibaoFavor::get([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ])) {
            $msg = 'ok';
        } else {
            $msg = 'false';
        }
        $this->result($msg);
    }

    public function add_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        $f = new HaibaoFavor();
        $f->data([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ]);
        $f->save();
        $this->success('ok');
    }

    public function del_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        $f = HaibaoFavor::get([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ]);
        if ($f) {
            $f->delete();
        }
        $this->success('ok');
    }

    public function get_favor_list()
    {
        $account_id = $this->request->post('user_id');
        $favor_list = HaibaoFavor::where('account_id', $account_id)->column('favor_id, createtime');
        $list = array();

        foreach ($favor_list as $id => $time) {
            $item = HaibaoVideo::get($id);
            if ($item) {
                $item->createtime = date('Y年m月d日', $time);
            }
        }

        $this->result('list', array_reverse($list));
    }

    // 处理收到formid,并创建发送任务
    public function handler_formid()
    {
        die();
    }
}
